import { createStore ,compose} from 'redux'
import rootReducer from '../reducers'

// compose 从右到左来组合多个函数; 
const finalCreateStore = compose(
  window.devToolsExtension ? window.devToolsExtension() : f => f
)(createStore);

export default function configureStore(initialState) {

  // createStore(reducer, [initialState]) 【 创建一个Redux store 来以存放应用中所有的 state。 应用中应有且仅有一个 store。】
  // 仍然相当于调用了 createStore; 
  const store = finalCreateStore(rootReducer, initialState);

  //热替换选项
  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextReducer = require('../reducers')
      store.replaceReducer(nextReducer)
    })
  }

  return store
}
